﻿using System;
using NSRE.Math;
using System.Collections.Generic;

namespace Mosos
{
    class AI_salaun_n: AI_interface
    {
        String image_path;
        Cell me;

        Vector2 acceleration;
        Vector2 speed;
        Vector2 force;

        Cell_interface prey;
        float waiting;


        public AI_salaun_n(string image_path)
        {
            this.image_path = image_path;
            waiting = 500;
        }

        public string get_path_image()
        {
            return image_path;
        }

        public void take_decision(Cell_interface[] them, Cell me)
        {
            this.me = me;
            Trap(them, me);
        }

        public void Trap(Cell_interface[] them, Cell me)
        {
            if (waiting <= 0)
                { waiting -= 16; }
            else
                Hunt(them, me);
        }

        public float Distance(Cell_interface c, Cell me)
        {
            this.me = me;

            float distance = 0;
            double dtotal = 0;
            float dradius = 0;

            Vector2 posme = me.getPosition();
            Vector2 posc = c.getPosition();

            float x = Math.Abs(posme.x - posc.x);
            float y = Math.Abs(posme.y - posc.y);
            dtotal = Math.Sqrt(Convert.ToDouble(x * x + y * y));
            dradius = -me.getRadius() - me.getRadius();

            distance = (float)dtotal + dradius;

            if (distance < 0)
                return distance;
            else
                return 0;
        }

        public Cell_interface BestChoice(Cell_interface[] them, Cell me)
        {
            float d = 0;
            float e = 0;
            float result = 0;
            Cell_interface bestprey = null;
            float bestcoef = 0;

            foreach (var c in them)
            {
                d = Distance(c, me);
                e = c.getEnergy();
                result = e / d;
                if ((c.getRadius() < (me.getRadius() - 20)) && (result > bestcoef))
                    {
                        bestcoef = result;
                        bestprey = c;
                    }
            }

            return bestprey;
        }

        public void Hunt(Cell_interface[] them, Cell me)
        {
            prey = BestChoice(them, me);
            acceleration = prey.getAcceleration();

            if (acceleration.x < 0)
                acceleration -= 10;
            else
                acceleration += 10;

            if (acceleration.y < 0)
                acceleration -= 10;
            else
                acceleration += 10;

            me.push(acceleration);
        }

        public void Move()
        {
        }
    }
}
